<?php
//栏目管理控制器
class categoryControl extends CommonControl{
	//添加栏目
	public function add(){
		if (IS_POST) {
			//p($_POST);die;
			$data = array(
				'cname'=>$this->_POST('cname'),
				'isoff'=>$this->_POST('isoff','intval'),
				'keyword'=>$this->_POST('keyword'),
				'description'=>$this->_POST('description'),
				);
			m('category')->add($data);
			$this->success('添加成功！！！');
			# code...
		}
		$this->display();
	}

	//查看栏目
	public function index(){
		$db = M('category');
		//分页
		$total = $db->count();
		$page = new page($total, 10, 4, 2);
		$this->assign('page',$page->show());

		$category = $db ->field('cid,cname,isoff')->order('cid DESC')->select($page->limit());
		$this->assign('category',$category);



		$this->display();
	}

	//开启和关闭栏目
	public function isoff(){
		$cid = $this->_GET('cid','intval');
		$w = $this->_GET('w','intval');

		if ($w==1) {
			M('category')->where(array('cid'=>$cid))->save(array('isoff'=>1));
			$this->success('关闭成功！！！');
			# code...
		}else{
			M('category')->where(array('cid'=>$cid))->save(array('isoff'=>0));
			$this->success('开启成功！！！');
		}
		
	}

	//编辑栏目
	public function edit(){
		if (IS_POST) {
			$date = array(
					'cname'			=>$this->_POST('cname'),
					'isoff'			=>$this->_POST('isoff','intval'),
					'keyword'		=>$this->_POST('keyword'),
					'description'	=>$this->_POST('description')
				);
			$cid = $this->_POST('cid','intval');
			M('category')->where(array('cid'=>$cid))->save($date);
			$this->success('修改成功！！！');
			# code...
		}
		$cid = $this->_GET('cid','intval');
		$category = M('category')->where(array('cid'=>$cid))->find();
		$this->assign('category',$category);
		$this->display();
	}

	//删除栏目
	public function del(){
		$cid = $this->_GET('cid','intval');
		M('category')->where(array('cid'=>$cid))->delete();

		$aid = M('article')->where(array('cid'=>$cid))->field('aid')->select();

		if($aid){
			$arr = array();
			foreach ($aid as $v) {
				$arr[] = $v['aid'];
				# code...
			}
			M('comment')->in(array('aid'=>$arr))->delete();

			M('article')->where(array('cid'=>$cid))->delete();
		}
		$this->success('删除成功！！！');
	}




}
?>